/*
 * Copyright 2007 David R Kirkland 
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");  
 * you may not use this file except in compliance with the License. 
 * You may obtain a copy of the License at 
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0 
 * 
 * Unless required by applicable law or agreed to in writing, software 
 * distributed under the License is distributed on an "AS IS" BASIS, 
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
 * See the License for the specific language governing permissions and 
 * limitations under the License.
 */

using System;
using System.Collections.Generic;
using System.Text;

namespace NetSpider.Retrieval.Policies
{
    /// <summary>
    /// 
    /// </summary>
    public class RetrievalDecision
    {
        private readonly FetchAction _action;
        private readonly DateTime _scheduleRetryFor;
        private readonly int _priority;
        private readonly string[] _errors;

        ///// <summary>
        ///// Instruction to cancel a fetch operation.
        ///// </summary>
        //public static readonly RetrievalDecision CancelFetch =
        //    new RetrievalDecision(FetchAction.CancelFetch, DateTime.MaxValue, 3);

        public static RetrievalDecision CancelFetch(string[] errors)
        {
            return new RetrievalDecision(FetchAction.RequeueItem, DateTime.MaxValue, 3, errors);
        }

        /// <summary>
        /// Instruction to continue a fetch operation.
        /// </summary>
        public static readonly RetrievalDecision ContinueFetching =
            new RetrievalDecision(FetchAction.ContinueFetching, DateTime.Now, 1, new string[0]);

        /// <summary>
        /// Instruction to continue a fetch operation.
        /// </summary>
        public static readonly RetrievalDecision Null =
            new RetrievalDecision(FetchAction.None, DateTime.MaxValue, 3, new string[0]);

        /// <summary>
        /// Instruction to requeue a fetch request.
        /// </summary>
        /// <param name="waitSeconds"></param>
        /// <param name="errors"></param>
        /// <returns></returns>
        public static RetrievalDecision RequeueItem(int waitSeconds, string[] errors)
        {
            return new RetrievalDecision(FetchAction.RequeueItem, DateTime.Now.AddSeconds(Convert.ToDouble(waitSeconds)), 2, errors);
        }

        private RetrievalDecision(FetchAction action, DateTime retryFetchTime, int priority, string[] errors)
        {
            _action = action;
            _scheduleRetryFor = retryFetchTime;
            _priority = priority;
            _errors = errors;
        }

        /// <summary>
        /// 
        /// </summary>
        public FetchAction Action
        {
            get { return _action; }
        }

        /// <summary>
        /// 
        /// </summary>
        public DateTime ScheduleRetryFor
        {
            get { return _scheduleRetryFor; }
        }

        public int Priority
        {
            get { return _priority; }
        }

        public string[] Errors
        {
            get { return _errors; }
        }
    }
}
